<?php

namespace Biz\service\package;

use Biz\module\Packages\Logistics\TrackingMore;
use Biz\model\package\OrderLogisticsLog as mOrderLogisticsLog;
use Doraemon\tools\Log as tLog;

class OrderLogisticsLog
{
    //回调更新物流信息及状态
    public static function callbackToLogistics($param)
    {
        try {
            $info = self::getLogisticsNo($param['data']['tracking_number']);
            if ($info) {
                $logisticsInfo['status'] = $param['data']['status'];
                $logisticsInfo['logistics_info'] = $param['data']['origin_info']['trackinfo'];
                $data = self::editToLogistics($param['data']['tracking_number'], $logisticsInfo);
                $res = mOrderLogisticsLog::editByOrderLogisticsNo($param['data']['tracking_number'], $data);
                tLog::info('editToLogistics', ['editToLogistics', 'callback', $data, $info, $param, $res]);
                if (empty($res)) {
                    throw new \Exception('积分商城的物流Webhook通知----(数据更新失败)');
                }
            } else {
                throw new \Exception('积分商城的物流Webhook通知----(数据库查不到此物流单号)');
            }
        } catch (\Exception $e) {
            tLog::warning('积分商城更新物流信息更新失败', [
                'logisticsData' => $param,
                'e_msg' => $e->getMessage(),
                'e_file' => $e->getFile(),
                'e_line' => $e->getLine()
            ]);
        }
    }

    //创建新的物流信息
    public static function add($param)
    {
        if (empty($param['uid']) || empty($param['order_no']) || empty($param['order_logistics_no']) || empty($param['carrier_code'])) {
            tLog::warning('添加物流单号---1.参数缺失', [$param]);
            return false;
        }
        //请求物流查询接口
        $res = TrackingMore::getTrackingResult($param['carrier_code'], $param['order_logistics_no']);
        if ($res && $res['meta']['code'] == 200 && $res['data']) {
            $param['status'] = $res['data']['status'];
            $param['logistics_info'] = $res['data']['origin_info']['trackinfo'];
        }
        $info = self::getLogisticsNo($param['order_logistics_no']);
        $data = self::editToLogistics($param['order_logistics_no'], $param);
        tLog::info('editToLogistics', ['editToLogistics', $data, $info, $param]);
        if ($info) {
            $result = mOrderLogisticsLog::editByOrderId($info['id'], $data);
        } else {
            $result = mOrderLogisticsLog::save($data);
        }
        if(empty($result)){
            tLog::warning('添加物流单号---2.入库操作失败', [$param]);
        }
        return $result;
    }

    public static function selectInfoByOrderNo($orderNo)
    {
        return mOrderLogisticsLog::selectInfo(['order_no' => $orderNo]);
    }

    public static function getLogisticsNo($LogisticsNo)
    {
        return mOrderLogisticsLog::getInfo(['order_logistics_no' => $LogisticsNo]);
    }

    public static function formatLogisticsInfo($data)
    {
        $logisticsInfo = [];
        if (is_array($data)) {
            foreach ($data as $val) {
                $logisticsInfo[] = [
                    'accept_station' => $val['StatusDescription'],
                    'accept_time' => $val['Date']
                ];
            }
            return json_encode($logisticsInfo, JSON_UNESCAPED_UNICODE);
        }
        return '';
    }

    //修改物流状态
    public static function editToLogistics($logisticsNo, $logisticsInfo)
    {
        $logisticsInfo['logistics_info'] = self::formatLogisticsInfo($logisticsInfo['logistics_info']);
        if (mOrderLogisticsLog::DELIVERED == $logisticsInfo['status']) {
            $result = \Biz\service\package\PackageOrder::logisticsUpdateStatus($logisticsInfo);
            if (empty($result)) {
                tLog::warning('新人大礼包更新物流信息--签收后更新失败', compact('logisticsNo', 'logisticsInfo'));
            }
        }
        return $logisticsInfo;
    }
}
